<?php
require("../services/class/conexion.php");
$link = mysql_connect($SERVIDOR, $USUARIO, $PASSWORD);
mysql_select_db($BASE, $link);
mysql_query("SET NAMES 'utf8'");

include "../libs/libchart/classes/libchart.php";

header("Content-type: image/png");

$dataSet = new XYSeriesDataSet();

$SELECT = mysql_query("SELECT tabla, descripcion FROM obras_sociales");
WHILE ($CONS = mysql_fetch_array($SELECT))
{
	$serie = "";
	$SEL_PAC_OS = mysql_query("
	SELECT DISTINCT
	(SELECT COUNT(fecha) FROM os_" . $CONS['tabla'] . " WHERE fecha BETWEEN '" . $_REQUEST['anio'] . "-01-01' AND '" . $_REQUEST['anio'] . "-01-31') as m01,  
	(SELECT COUNT(fecha) FROM os_" . $CONS['tabla'] . " WHERE fecha BETWEEN '" . $_REQUEST['anio'] . "-02-01' AND '" . $_REQUEST['anio'] . "-02-31') as m02,
	(SELECT COUNT(fecha) FROM os_" . $CONS['tabla'] . " WHERE fecha BETWEEN '" . $_REQUEST['anio'] . "-03-01' AND '" . $_REQUEST['anio'] . "-03-31') as m03,
	(SELECT COUNT(fecha) FROM os_" . $CONS['tabla'] . " WHERE fecha BETWEEN '" . $_REQUEST['anio'] . "-04-01' AND '" . $_REQUEST['anio'] . "-04-31') as m04,
	(SELECT COUNT(fecha) FROM os_" . $CONS['tabla'] . " WHERE fecha BETWEEN '" . $_REQUEST['anio'] . "-05-01' AND '" . $_REQUEST['anio'] . "-05-31') as m05,
	(SELECT COUNT(fecha) FROM os_" . $CONS['tabla'] . " WHERE fecha BETWEEN '" . $_REQUEST['anio'] . "-06-01' AND '" . $_REQUEST['anio'] . "-06-31') as m06,
	(SELECT COUNT(fecha) FROM os_" . $CONS['tabla'] . " WHERE fecha BETWEEN '" . $_REQUEST['anio'] . "-07-01' AND '" . $_REQUEST['anio'] . "-07-31') as m07,
	(SELECT COUNT(fecha) FROM os_" . $CONS['tabla'] . " WHERE fecha BETWEEN '" . $_REQUEST['anio'] . "-08-01' AND '" . $_REQUEST['anio'] . "-08-31') as m08,
	(SELECT COUNT(fecha) FROM os_" . $CONS['tabla'] . " WHERE fecha BETWEEN '" . $_REQUEST['anio'] . "-09-01' AND '" . $_REQUEST['anio'] . "-09-31') as m09,
	(SELECT COUNT(fecha) FROM os_" . $CONS['tabla'] . " WHERE fecha BETWEEN '" . $_REQUEST['anio'] . "-10-01' AND '" . $_REQUEST['anio'] . "-10-31') as m10,
	(SELECT COUNT(fecha) FROM os_" . $CONS['tabla'] . " WHERE fecha BETWEEN '" . $_REQUEST['anio'] . "-11-01' AND '" . $_REQUEST['anio'] . "-11-31') as m11,
	(SELECT COUNT(fecha) FROM os_" . $CONS['tabla'] . " WHERE fecha BETWEEN '" . $_REQUEST['anio'] . "-12-01' AND '" . $_REQUEST['anio'] . "-12-31') as m12
	FROM os_" . $CONS['tabla'] . "");

	$MESES = mysql_fetch_array($SEL_PAC_OS); 
	$serie = new XYDataSet();
	FOR ($i=1; $i<=12; $i++)
	{
		if ($i<=9)
		{
			$serie->addPoint(new Point("0".$i, $MESES["m0".$i]));
		}
		else
		{
			$serie->addPoint(new Point($i, $MESES["m".$i]));
		}
	}
	$dataSet->addSerie($CONS['descripcion'], $serie);
}
$chart = new LineChart();
$chart->setDataSet($dataSet);

$chart->setTitle("Pacientes del año " . $_REQUEST['anio']);
$chart->getPlot()->setGraphCaptionRatio(0.6);
$chart->render();
?>